﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows;

namespace ProfilerPDS {

    public enum ConnectionType {
        [Description("Function Call")]
        FunctionCall,
        [Description("Function Return")]
        FunctionReturn
    }

    class Connection {

        private Arrow _arrow;
        public Arrow  Arrow {
            get { return _arrow; }
        }

        private Label _label;
        public Label Label {
            get { return _label; }
        }

        private int _zIndex;
        public int zIndex
        {
            get { return _zIndex; }
            set { _zIndex = value; }
        }


        public Connection() {

        }


        public void newConnection(double X1,double Y1,double X2,double Y2, ConnectionType cT,string label) {

            Arrow arrow=  new Arrow();
            arrow.X1=X1;
            arrow.X2=X2;
            arrow.Y1=Y1;
            arrow.Y2=Y2;
            arrow.Stroke=Brushes.Blue; 
            arrow.StrokeThickness=1.0;
            arrow.HeadHeight=7;
            arrow.HeadWidth=7;
            _arrow = arrow;

            Label txtL = new Label();
            //txtL.TextWrapping = TextWrapping.NoWrap;
            txtL.Content = label+"()";
            txtL.Foreground = Brushes.Black;
            txtL.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
            _label = txtL;


            switch (cT) {
                case ConnectionType.FunctionCall:
                    Canvas.SetLeft(txtL, X2-arrow.HeadWidth-txtL.DesiredSize.Width-7);
                    Canvas.SetTop(txtL, Y2-txtL.DesiredSize.Height+3);
                    break;
                case ConnectionType.FunctionReturn:
                    Canvas.SetLeft(txtL, X2-arrow.HeadWidth-txtL.DesiredSize.Width-7);
                    Canvas.SetTop(txtL, Y2-txtL.DesiredSize.Height+3);
                    arrow.RenderTransform = new RotateTransform(180, X1+((X2-X1)/2), Y1);
                    arrow.StrokeDashArray.Insert(0, 5);
                    arrow.StrokeDashArray.Insert(1, 3);
                    break;
                default:
                    break;
            }

            Canvas.SetZIndex(arrow, _zIndex);
            Canvas.SetZIndex(txtL, _zIndex);

        }

        public void add(Canvas drawArea) {
            Canvas.SetZIndex(this.Arrow, _zIndex);
            Canvas.SetZIndex(this.Label, _zIndex);
            drawArea.Children.Add(this.Arrow);
            drawArea.Children.Add(this.Label);
        }

        public void remove(Canvas drawArea) {
            drawArea.Children.Remove(this.Arrow);
            drawArea.Children.Remove(this.Label);
        }




    }
}
